<template>
    <div class="header">
        <!-- 折叠按钮 -->
        <div class="collapse-btn" @click="collapseChage">
            <i v-if="!collapse" class="el-icon-s-fold"></i>
            <i v-else class="el-icon-s-unfold"></i>
        </div>
        <div class="logo">课程系统</div>
        <div class="header-right">
            <div class="header-user-con">
                <!-- 全屏显示 -->
                <div class="btn-fullscreen" @click="handleFullScreen">
                    <el-tooltip effect="dark" :content="fullscreen ? `取消全屏` : `全屏`" placement="bottom">
                        <i class="el-icon-rank"></i>
                    </el-tooltip>
                </div>
                <!-- 消息中心 -->
                <div class="btn-bell">
                    <el-tooltip effect="dark" :content="message ? `有${message}条未读消息` : `消息中心`" placement="bottom">
                        <router-link to="/tabs">
                            <i class="el-icon-bell"></i>
                        </router-link>
                    </el-tooltip>
                    <span class="btn-bell-badge" v-if="message"></span>
                </div>
                <!-- 用户头像 -->
                <div class="user-avator">
                    <router-link to="/upload"> <img :src="imageURL" :title="updateImage" /></router-link>
                </div>
                <!-- 用户名下拉菜单 -->
                <el-dropdown class="user-name" trigger="click" @command="handleCommand">
                    <span class="el-dropdown-link">
                        {{ name }}
                        <i class="el-icon-caret-bottom"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="updateData"> 修改资料</el-dropdown-item>
                        <el-dropdown-item divided command="loginout">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <!-- 修改资料弹出框 -->
                <el-dialog title="修改资料" :visible.sync="updateData" width="30%">
                    <el-form :model="updata" label-width="100px" :rules="rules">
                        <el-form-item label="电话：" prop="studentPhone">
                            <el-input size="large" v-model="updata.studentPhone" placeholder="请输入您的电话"></el-input>
                        </el-form-item>
                        <el-form-item label="邮箱：" prop="studentEmail">
                            <el-input size="large" v-model="updata.studentEmail" placeholder="请输入您的邮箱"></el-input>
                        </el-form-item>
                    </el-form>
                    <span slot="footer" class="dialog-footer">
                        <el-button type="primary" @click="updataOneslef()">确定</el-button>
                        <el-button type="danger" @click="NoUpdata()">取消</el-button>
                    </span>
                </el-dialog>
            </div>
        </div>
    </div>
</template>
<script>
import bus from '../common/bus';
export default {
    data() {
        return {
            updata: {
                studentNumber: localStorage.getItem('ms_number'),
                studentPhone: '',
                studentEmail: ''
            },
            rules: {
                studentPhone: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    // 这个只能验证手机号
                    // { pattern:/^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/, message: "请输入合法手机号", trigger: "blur" }
                    { pattern: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/, message: '请输入合法手机号/电话号', trigger: 'blur' }
                ],

                studentEmail: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { pattern: /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g, message: '请输入正确的邮箱', trigger: 'blur' }
                ]
            },
            updateImage: '修改头像',
            updateData: false,
            imageURL: localStorage.getItem('imageURL'),
            collapse: false,
            fullscreen: false,
            name: localStorage.getItem('ms_name'),
            message: 2
        };
    },
    computed: {
        // username() {
        //     let username = localStorage.getItem('ms_name');
        //     return username ? username : this.name;
        // }
    },
    methods: {
        // 用户名下拉菜单选择事件
        handleCommand(command) {
            if (command == 'loginout') {
                // localStorage.removeItem('ms_name');
                // localStorage.removeItem('ms_role');
                // localStorage.removeItem('imageURL');
                window.localStorage.clear();
                this.$router.push('/login');
            }
            if (command == 'updateData') {
                this.updateData = true;
            }
        },
        //更改个人信息
        updataOneslef() {
            console.log(this.updata);
            this.$http.post('http://localhost:9900/update/updateData', this.updata).then((res) => {
                if (res.data == 'success!') {
                    this.updateData = false;
                    this.$message.success('修改成功');
                } else {
                    this.updateData = false;
                    this.$message.error('修改失败，请稍后再试');
                }
                (this.updateData = false), (this.updata.studentPhone = ''), (this.updata.studentEmail = '');
            });
        },
        //取消更新个人资料
        NoUpdata() {
            (this.updateData = false), (this.updata.studentPhone = ''), (this.updata.studentEmail = '');
        },
        // 侧边栏折叠
        collapseChage() {
            this.collapse = !this.collapse;
            bus.$emit('collapse', this.collapse);
        },
        // 全屏事件
        handleFullScreen() {
            let element = document.documentElement;
            if (this.fullscreen) {
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                } else if (document.webkitCancelFullScreen) {
                    document.webkitCancelFullScreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if (document.msExitFullscreen) {
                    document.msExitFullscreen();
                }
            } else {
                if (element.requestFullscreen) {
                    element.requestFullscreen();
                } else if (element.webkitRequestFullScreen) {
                    element.webkitRequestFullScreen();
                } else if (element.mozRequestFullScreen) {
                    element.mozRequestFullScreen();
                } else if (element.msRequestFullscreen) {
                    // IE11
                    element.msRequestFullscreen();
                }
            }
            this.fullscreen = !this.fullscreen;
        }
    },
    mounted() {
        if (document.body.clientWidth < 1500) {
            this.collapseChage();
        }
    }
};
</script>
<style scoped>
.header {
    position: relative;
    box-sizing: border-box;
    width: 100%;
    height: 70px;
    font-size: 22px;
    color: #fff;
}
.collapse-btn {
    float: left;
    padding: 0 21px;
    cursor: pointer;
    line-height: 70px;
}
.header .logo {
    float: left;
    width: 250px;
    line-height: 70px;
}
.header-right {
    float: right;
    padding-right: 50px;
}
.header-user-con {
    display: flex;
    height: 70px;
    align-items: center;
}
.btn-fullscreen {
    transform: rotate(45deg);
    margin-right: 5px;
    font-size: 24px;
}

.btn-bell,
.btn-fullscreen {
    position: relative;
    width: 30px;
    height: 30px;
    text-align: center;
    border-radius: 15px;
    cursor: pointer;
}
.btn-bell-badge {
    position: absolute;
    right: 0;
    top: -2px;
    width: 8px;
    height: 8px;
    border-radius: 4px;
    background: #f56c6c;
    color: #fff;
}
.btn-bell .el-icon-bell {
    color: #fff;
}
.user-name {
    margin-left: 10px;
}
.user-avator {
    margin-left: 20px;
    font-size: 24px;
}
.user-avator img {
    display: block;
    width: 40px;
    height: 40px;
    border-radius: 50%;
}
.el-dropdown-link {
    color: #fff;
    cursor: pointer;
}
.el-dropdown-menu__item {
    text-align: center;
}
</style>
